//index.js
//获取应用实例
var md5 = require('../../utils/md5.js');
var app = getApp()
Page({
  data: {
    talk: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false , // loading
    scrollTop:"0",
    loadingMoreHidden:true,
    hasNoCoupons:true,
    page: 1,
    page_count: 10,
    keyword: '',
    loadmore: true,
    keywords:'',
    isFirst: true
  },

  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  //事件处理函数
  swiperchange: function(e) {
      //console.log(e.detail.current)
       this.setData({  
        swiperCurrent: e.detail.current  
    })  
  },
  skipMap:function(e){//返回键跳转到地图页
    console.log(e)
    //  wx.switchTab({
    //    url: "pages/map/map"
    //  })
  },
  imgYu: function (e) {//图片预览
    var src = e.currentTarget.dataset.src;//获取data-src
    var imgList = e.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  tapDetails: function(e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/tansuo-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  bindTypeTap: function(e) {
     this.setData({  
        selectCurrent: e.index  
    })  
  },
  scroll: function (e) {
    //  console.log(e) ;
    var that = this,scrollTop=that.data.scrollTop;
    that.setData({
      scrollTop:e.detail.scrollTop
    })
    // console.log('e.detail.scrollTop:'+e.detail.scrollTop) ;
    // console.log('scrollTop:'+scrollTop)
  },
  onLoad: function () {
    var that = this;
    that.setData({
      page: 1
    })
    that.getTalk(); 
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触发上拉')
    if (this.data.keyword == '') {
      if (this.data.loadmore) {
        var page = Number(this.data.page) + Number(1);
        console.log(page)
        this.setData({
          page: page
        });
        this.getTalk()
      } else {
        wx.showToast({
          title: '暂无更多'
        })
      }
    }
  },
  /**
   * 页面下拉事件的处理函数
   */
  onPullDownRefresh :function(){
    console.log('触发下拉')
    this.setData({
      page: 1
    });
    this.getTalk()
    wx.stopPullDownRefresh()
  },
  getTalk:function(){
    var that = this;
    var isFirst = that.data.isFirst;
    if (isFirst) {
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      })
    }
    var service = 'Talk.GetList'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var center_jingwei = wx.getStorageSync('latitude') + ',' + wx.getStorageSync('longitude')
    var length = 3000
    var page = that.data.page
    var page_count = that.data.page_count
    var keywords = that.data.keywords
    var sign = md5.md5(center_jingwei + keywords + length + open_id + page + page_count + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        center_jingwei: center_jingwei,
        length: length,
        page: page,
        page_count: page_count,
        keywords: keywords
      },
      success: function (res) {
        wx.hideToast()
        console.log(res.data.data.info)
        var talk = []
        if (res.data.data.code == 0) {
          if (page > 1) {
            talk = that.data.talk.concat(res.data.data.info)
          } else {
            talk = res.data.data.info
          }
          that.setData({
            talk: talk,
            loadmore: true,
            keywords: '',
            isFirst: false
          });
        }else{
          that.setData({
            talk: talk,
            loadmore: true,
            keywords: '',
            isFirst: false
          });
        }
      },
      fail: function (res) {
        wx.hideToast()
      }
    })
  },
  onCollectionTap:function(e){
    var that = this;
    var key = e.target.dataset.index;
    if (e.currentTarget.dataset.type == 0){
      var collect_val = 1
      var talk_zan_count = that.data.talk[key].talk_zan_count * 1 + 1
    }else{
      var collect_val = 0
      var talk_zan_count = that.data.talk[key].talk_zan_count * 1 - 1
    }
    var service = 'Talk.Zan'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var talk_id = e.currentTarget.dataset.id
    var collect = collect_val
    var sign = md5.md5(collect + open_id + service + talk_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        talk_id: talk_id,
        collect: collect
      },
      success: function (res) {
        if (res.data.data.code == 0) {
          // that.setData({
          //   talk: [],
          // });
          // that.getTalk()
          that.data.talk[key].talk_collect = collect_val;
          that.data.talk[key].talk_zan_count = talk_zan_count;
          that.setData({
            talk: that.data.talk,
            loadmore: true,
          });
        }
      }
    })
  },

  wxSearchInput: function (e) {
    this.setData({
      keywords: e.detail.value
    })
  },
  wxSearchFn: function () {
    wx.showToast({
      title: '搜索中',
      icon: 'loading'
    })
    this.setData({
      page: 1
    });
    this.getTalk()
  }
})
